//
//  RepairController.m
//  智慧小区
//
//  Created by lab603 on 15/6/30.
//  Copyright (c) 2015年 Dreamsoul. All rights reserved.
//

#import "RepairController.h"
#import "PPHttpTool.h"
#import "Repair.h"
#import "MJExtension.h"
#import "APIClient.h"
#import "RepairHistoryCell.h"
#import "RepairFrame.h"
#import "MJRefresh.h"
#import "Tool.h"
#import "RepairDetailController.h"
#import "RepairNewController.h"
@interface RepairController ()
//请求数据的页面值
@property (nonatomic,assign)int page;
//数据模型数组
@property (nonatomic,strong)NSMutableArray *repairArray;
@end

@implementation RepairController
- (NSMutableArray *)repairArray{
    if (_repairArray==nil) {
        _repairArray = [NSMutableArray array];
    }
    return _repairArray;
}
- (id)init{
    self = [super init];
    if (self) {
        self.title = @"报修记录";
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addNewRepair)];
        self.view.backgroundColor = APP_BACK_COLOR;
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化页面值
    self.page = 0;
    //获取报修历史数据
    [self getRepairData];
    //添加下拉刷新
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getRepairData)];
    //添加上拉刷新
    self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(getMoreRepairData)];
}
/**
 *  获得更多的数据
 */
- (void)getMoreRepairData{
    NSString *urlString = [APIClient relativePathForRepairHistory];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"page"] = @(++self.page);
    params[@"size"] = @10;
    [PPHttpTool getWithURL:urlString params:params success:^(id json) {
        NSArray *tempArray = [Repair objectArrayWithKeyValuesArray:json];
        if (tempArray.count==0) {
            [Tool HUDShowMessage:@"没有更多记录"];
            [self.tableView.footer endRefreshing];
            return ;
        }
        for (int i=0; i<tempArray.count; i++) {
            Repair *repair = tempArray[i];
            RepairFrame *repairF = [[RepairFrame alloc] init];
            repairF.repair = repair;
            [self.repairArray addObject:repairF];
        }
        [self.tableView.footer endRefreshing];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        [self.tableView.footer endRefreshing];
    }];
}
/**
 *  获取报修历史数据
 */
- (void)getRepairData{
    NSString *urlString = [APIClient relativePathForRepairHistory];
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    self.page = 1;
    params[@"page"] = @(self.page);
    params[@"size"] = @10;
    [PPHttpTool getWithURL:urlString params:params success:^(id json) {
        IWLog(@"%@",json);
        NSArray *tempArray = [Repair objectArrayWithKeyValuesArray:json];
        [self.repairArray removeAllObjects];
        for (int i=0; i<tempArray.count; i++) {
            Repair *repair = tempArray[i];
            RepairFrame *repairF = [[RepairFrame alloc] init];
            repairF.repair = repair;
            [self.repairArray addObject:repairF];
        }
        if ([self.tableView.header isRefreshing]) {
            [self.tableView.header endRefreshing];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        if ([self.tableView.header isRefreshing]) {
            [self.tableView.header endRefreshing];
        }
    }];
}
/**
 *  添加报修
 */
- (void)addNewRepair{
    RepairNewController *newRepairVC = [[RepairNewController alloc] init];
    [self.navigationController pushViewController:newRepairVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.repairArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RepairHistoryCell *cell = [RepairHistoryCell cellWithTableView:tableView];
    RepairFrame *repairF = self.repairArray[indexPath.row];
    cell.repairFrame = repairF;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    RepairFrame *repairF = self.repairArray[indexPath.row];
    return repairF.cellHeight;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    RepairFrame *repairF = self.repairArray[indexPath.row];
    Repair *repair = repairF.repair;
    RepairDetailController *repairDetailVC = [[RepairDetailController alloc] init];
    repairDetailVC.repair = repair;
    [self.navigationController pushViewController:repairDetailVC animated:YES];
}
@end
